package books.in_action.highFunction

import books.in_action.lambda.Person

fun main(args: Array<String>) {

    "abbbbdbbdbdbasdjksal".copyFilter { it != 'b' }.also(::println)

    listOf(Person(11,"88"),Person(22,"66")).copyJoinToString("\n").also(::println)

}


fun String.copyFilter(leaveAction: (Char) -> Boolean): String {
    val buffer = StringBuffer()
    for (index in 0 until length) {
        val element = get(index)
        if (leaveAction(element)) buffer.append(element)
    }
    return buffer.toString()
}

fun <T> Collection<T>.copyJoinToString(
        separator: String = ",",
        prefix: String = "",
        postfix: String = "",
        //给Lambda参数类型参数指定默认值
        transform: (T) -> String = { it.toString() }
): String {
    val buffer = StringBuffer()
    buffer.append(prefix)
    for ((index, element) in this.withIndex()) {
        if (index > 0) buffer.append(separator)
        buffer.append(transform(element))
    }
    buffer.append(postfix)
    return buffer.toString()
}


fun javaInvokeKotlin(action: (Int) -> Unit) {
    println(action(42))
}

